package com.springchang.leetcode.sword2offer;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * 剑指 Offer 05. 替换空格
 * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 */
public class Offer05 {
    public String replaceSpace(String s) {
        char[] chars = new char[s.length()*3];

        int size = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c == ' ') {
                chars[size++] = '%';
                chars[size++] = '2';
                chars[size++] = '0';
            } else {
                chars[size++] = c;
            }
        }

        return new String(chars, 0, size);
    }

    public static void main(String[] args) {
        Offer05 testCase = new Offer05();
        System.out.println(testCase.replaceSpace("We are happy.").equals("We%20are%20happy."));
        System.out.println(testCase.replaceSpace("   ").equals("We%20are%20happy."));
    }
}
